package com.angrysnail.cloud.test.consumer;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
	@Autowired
	private DiscoveryClient client;

	@Autowired
	private RestTemplate template;

	@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
	public String consume() {
		List<ServiceInstance> instances = client.getInstances("hello-service1");
		for (ServiceInstance serviceInstance : instances) {
			String host = serviceInstance.getHost();
			int port = serviceInstance.getPort();
			String ret = template.getForEntity("http://hello-service1/hello", String.class).getBody();
			return ret;
		}
		return "==";
	}
}